package com.fyyg.common.util;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

/**
 * 针对百度WebUploader上传插件上传文件帮助类
 * @author zdx
 * @date 2016年4月9日 14:03 
 *
 */
public class  WebUploaderUtil {
	
	private static String allowSuffix = "gif,jpg,jpeg,bmp,png";//允许文件格式
    private static long allowSize = 2L;//允许文件大小
    private static String fileName;
    private static String[] fileNames;
     
    public static String getAllowSuffix() {
        return allowSuffix;
    }
 
    public void setAllowSuffix(String allowSuffix) {
        this.allowSuffix = allowSuffix;
    }
 
    public static long getAllowSize() {
        return allowSize*1024*1024;
    }
 
    public void setAllowSize(long allowSize) {
        this.allowSize = allowSize;
    }
 
    public static String getFileName() {
        return fileName;
    }
 
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
 
    public String[] getFileNames() {
        return fileNames;
    }
 
    public void setFileNames(String[] fileNames) {
        this.fileNames = fileNames;
    }
 
     
 
    /**
     * 
     * <p class="detail">
     * 功能：重新命名文件
     * </p>
     * @return
     */
    private static  String getFileNameNew(){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return fmt.format(new Date());
    }
     
    /**
     * 
     * <p class="detail">
     * 功能：文件上传
     * </p>
     * @param files
     * @param destDir
     * @throws Exception
     */
    public static void uploads(MultipartFile[] files, String destDir,HttpServletRequest request) throws Exception {
        String path = request.getContextPath();
        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
        try {
            fileNames = new String[files.length];
            int index = 0;
            for (MultipartFile file : files) {
                String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
                int length = getAllowSuffix().indexOf(suffix);
                if(length == -1){
                    throw new Exception("请上传允许格式的文件");
                }
                if(file.getSize() > getAllowSize()){
                    throw new Exception("您上传的文件大小已经超出范围");
                }
                String realPath = request.getSession().getServletContext().getRealPath("/");
                File destFile = new File(realPath+destDir);
                if(!destFile.exists()){
                    destFile.mkdirs();
                }
                String fileNameNew = getFileNameNew()+"."+suffix;//
                File f = new File(destFile.getAbsoluteFile()+"\\"+fileNameNew);
                file.transferTo(f);
                f.createNewFile();
                fileNames[index++] =basePath+destDir+fileNameNew;
            }
        } catch (Exception e) {
            throw e;
        }
    }
     
    /**
     * 
     * <p class="detail">
     * 功能：文件上传
     * </p>
     * @param files
     * @param destDir
     * @throws Exception
     */
    public static void upload(MultipartFile file, String destDir,HttpServletRequest request) throws Exception {
    	String path = request.getContextPath();
    	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
        try {
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
            int length = getAllowSuffix().indexOf(suffix);
            if(length == -1){
                throw new Exception("请上传允许格式的文件");
            }
            if(file.getSize() > getAllowSize()){
                throw new Exception("您上传的文件大小已经超出范围");
            }
             
            String realPath = request.getSession().getServletContext().getRealPath("/");
            File destFile = new File(realPath+destDir);
            if(!destFile.exists()){
                destFile.mkdirs();
            }
            String fileNameNew = getFileNameNew()+"."+suffix;
            File f = new File(destFile.getAbsoluteFile()+"/"+fileNameNew);
            file.transferTo(f);
            f.createNewFile();
            fileName = basePath+destDir+fileNameNew;
       } catch (Exception e) {
         throw e;
       }
  }
}
